﻿using System;
using PMS.SANY.Entity;
using PMS.SANY.IBusiness;
using PMS.SANY.Models;
using PMS.SANY.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace PMS.SANY.Api.Controllers
{
    /// <summary>
    /// MaterielSortingSequence
    /// </summary>
    public class MaterielSortingSequenceController : BaseController
    {
        /// <summary>
        /// 
        /// </summary>
        public IMaterielSortingSequenceBusiness Bus { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public IMaterielSortingConfigBusiness ConfigBus { set; get; }
        /// <summary>
        /// 
        /// </summary>
        public IServiceProvider SvcProvider { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bus"></param>
        /// <param name="configBus"></param>
        /// <param name="svcProvider"></param>
        public MaterielSortingSequenceController(IMaterielSortingSequenceBusiness bus, IMaterielSortingConfigBusiness configBus, IServiceProvider svcProvider)
        {
            this.Bus = bus;
            this.ConfigBus = configBus;

            this.SvcProvider = svcProvider;
        }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public Task<MaterielSortingSequence> GetAsync(string id)
        {
            return this.Bus.GetAsync(id, true);
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public Task<PageResult<MaterielSortingSequence>> GetPageAsync(PageInput<MaterielSortingSequenceQM> query)
        {
            return this.Bus.GetPageResultAsync(query);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="data">对应数据</param>
        /// <returns></returns>
        [HttpPost]
        [Transaction]
        public Task<int> SaveAsync(MaterielSortingSequence data)
        {
            this.InitBusEntity(data);
            data.OrderNo = this.Bus.GetQueryable().Where(q => q.SortingConfigId == data.SortingConfigId).Count() + 1;

            return this.Bus.AddOrUpdateAsync(data);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids">数据主键</param>
        /// <returns></returns>
        [HttpDelete]
        [Transaction]
        public Task<int> DeleteAsync(List<string> ids)
        {
            return this.Bus.RemoveAsync(ids);
        }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public Task<int> UpAsync(string id)
        {
            return this.Bus.SortingAsync(id, true);
        }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public Task<int> DownAsync(string id)
        {
            return this.Bus.SortingAsync(id, false);
        }
    }
}
